package com.aidex.web.controller.biz;

import java.util.List;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.biz.domain.UserCoupon;
import com.aidex.biz.service.UserCouponService;
import com.aidex.common.utils.SecurityUtils;

/**
 * 用户领券信息Controller
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-07-02
 */
@RestController
@RequestMapping("/biz/bizUserCoupon")
public class UserCouponController extends BaseController
{
    @Autowired
    private UserCouponService userCouponService;

    /**
     * 查询用户领券信息列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserCoupon:list')")
    @GetMapping("/list")
    public R<PageInfo> list(UserCoupon userCoupon, HttpServletRequest request, HttpServletResponse response)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            userCoupon.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
         }
        userCoupon.setPage(new PageDomain(request, response));
        return R.data(userCouponService.findPage(userCoupon));
    }

    /**
     * 获取用户领券信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserCoupon:query')")
    @GetMapping(value = "/{id}")
    public R<UserCoupon> detail(@PathVariable("id") String id)
    {
        return R.data(userCouponService.get(id));
    }

    /**
     * 新增用户领券信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserCoupon:add')")
    @Log(title = "用户领券信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  UserCoupon userCoupon)
    {
        return R.status(userCouponService.save(userCoupon));
    }

    /**
     * 修改用户领券信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserCoupon:edit')")
    @Log(title = "用户领券信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated UserCoupon userCoupon)
    {
        return R.status(userCouponService.save(userCoupon));
    }


    /**
     * 删除用户领券信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserCoupon:remove')")
    @Log(title = "用户领券信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        return R.status(userCouponService.deleteUserCouponByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserCoupon:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort()
    {
        return R.data(userCouponService.findMaxSort(new UserCoupon()));
    }

    /**
     * 导出用户领券信息列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserCoupon:export')")
    @Log(title = "用户领券信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(UserCoupon userCoupon)
    {

            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                    userCoupon.setCompanyId(null);
            }else{
                         userCoupon.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                     }
        List<UserCoupon> list = userCouponService.findList(userCoupon);
        ExcelUtil<UserCoupon> util = new ExcelUtil<UserCoupon>(UserCoupon.class);
        return util.exportExcel(list, "用户领券信息数据");
    }

}
